package com.gyms.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.okhttp.beans.HVCardBean;
import com.gyms.R;
import com.gyms.activity.CouponDetailActivity;
import com.gyms.constant.Constants;
import com.gyms.manager.StateUtils;
import com.gyms.view.bottomview.ShareDialog;

import java.util.ArrayList;

import click.OnRecyclerViewItemClick;
import constant.PicSplit;
import utils.DateUtil;
import utils.DecimalUtils;
import utils.StringUtils;

/**
 * Created by ltx on 17/5/25
 *我的卡卷适配器
 */

public class MyCouponAdapterNew extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<HVCardBean> mHVCardBeans;//优惠券bean
    private boolean isMyCoupon;//是否我的卡卷页面 true 我的卡卷页面 false 订单卡卷选择页面

    private OnRecyclerViewItemClick mOnRecyclerViewItemClick;


    private static final int S_HISTORY_TYPE=0;//历史的分割线
    private static final int S_NORMAL_TYPE=1;//普通卡卷
    //分享口令
    private ShareDialog mShareDialog;

    /**
     * @param mContext context
     * @param isMyCoupon 是否我的卡卷页面 true 我的卡卷页面 false 订单卡卷选择页面
     */
    public MyCouponAdapterNew(Context mContext, boolean isMyCoupon){
        this.mContext=mContext;
        this.isMyCoupon=isMyCoupon;
        mInflater=LayoutInflater.from(mContext);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder mHolder=null;
        switch (viewType){
            case S_HISTORY_TYPE:
                View mHistoryView= mInflater.inflate(R.layout.ll_coupon_history,parent,false);
                mHolder=new HistoryViewHolder(mHistoryView);
                break;

            case S_NORMAL_TYPE:
                View mNomView = mInflater.inflate(R.layout.ll_coupon_item,parent,false);
                mHolder=new CouponViewHolder(mNomView);
                break;
        }

        return mHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(utils.StringUtils.isNull(mHVCardBeans)){
            return;
        }
        int mItemType=getItemViewType(position);
        switch (mItemType){
            case S_HISTORY_TYPE://历史订单标识

                break;

            case S_NORMAL_TYPE:
                CouponViewHolder mCouponHolder= (CouponViewHolder) holder;
                HVCardBean mHvCardBean=mHVCardBeans.get(position);
                doFullCard(mHvCardBean,mCouponHolder);
                mCouponHolder.itemView.setTag(position);
                mCouponHolder.mTvShareOrder.setTag(position);
                mCouponHolder.mIvIsCanDetail.setTag(position);
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        HVCardBean mTempCardBean=mHVCardBeans.get(position);
        boolean mIsHaveHistory=mTempCardBean.getIsHaveHistory();

        return mIsHaveHistory?S_HISTORY_TYPE:S_NORMAL_TYPE;
    }

    /**
     *  @param mHvCardBean 卡卷bean
     * @param mCouponViewHolder holder
     */

    private void doFullCard(HVCardBean mHvCardBean, CouponViewHolder mCouponViewHolder) {
        mCouponViewHolder.mTvCouponMoney.setText(DecimalUtils.doubleToIntOrD(mHvCardBean.money));
        StateUtils.setCouponUseMoneyStr(mContext,mCouponViewHolder.mTvCouponMoneyUse,mHvCardBean.condition);//卡卷使用的条件
        String mStrCouponDate=String.format(mContext.getString(R.string.str_coupon_date),DateUtil.getSimpleDateNoS(mHvCardBean.getStartTime()),DateUtil.getSimpleDateNoS(mHvCardBean.getEndTime()));
        mCouponViewHolder.mTvCouponDate.setText(mStrCouponDate);
        mCouponViewHolder.mTvCouponUseRule.setText(mHvCardBean.content);
        mCouponViewHolder.mTvCouponName.setText(mHvCardBean.couponName);
        StateUtils.setCardIsSelect(mCouponViewHolder.mIvCouponChose,mHvCardBean.isSelcted,mHvCardBean.exclusive);//卡卷选择逻辑
        if(!isMyCoupon){//我的订单选择页面
            StateUtils.doShowIsCanUseState(mContext,mCouponViewHolder,mHvCardBean.getIsUseAble(),mHvCardBean.exclusive);
        }else{
            StateUtils.showCouponState(mContext,mCouponViewHolder,mHvCardBean.getIsWork(),mHvCardBean.exclusive);
        }
    }

    @Override
    public int getItemCount() {
        return utils.StringUtils.isNull(mHVCardBeans)?0:mHVCardBeans.size();
    }

    /**
     * 设置数据源
     * @param HVCardBeans 优惠券bean
     */

    public void setHVCardBeans(ArrayList<HVCardBean> HVCardBeans) {
        mHVCardBeans = HVCardBeans;
    }

    public void setOnRecyclerViewItemClick(OnRecyclerViewItemClick onRecyclerViewItemClick) {
        mOnRecyclerViewItemClick = onRecyclerViewItemClick;
    }

    public class CouponViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
       public TextView mTvCouponMoney;
       public TextView mTvCouponMoneyUse;
       public TextView mTvCouponUseRule;
       private ImageView mIvCouponChose;
       private TextView mTvCouponDate;
       public TextView mTvShareOrder;//卡卷分享
       public TextView mTvCouponName;//卡卷名称
       public ImageView mIvCanUse;// 不可使用的角标
       public ImageView mIvIsCanDetail;// 是否可参看详情
       public FrameLayout mFlCouponMoney;//优惠券减额的背景
       public RelativeLayout mRlCouponContent;
       public ImageView mIvMoneyIcon;//人民币符号
       public ImageView mIvCouponUseRulePoint;//使用规则的小点
       public ImageView mIvCouponMoneyUsePoint;//使用的条件的小点
        public LinearLayout mLlCouponName;
        private CouponViewHolder(View itemView) {
            super(itemView);
            mTvCouponMoney= (TextView) itemView.findViewById(R.id.tv_coupon_money);
            mTvCouponMoneyUse= (TextView) itemView.findViewById(R.id.tv_coupon_money_use);
            mTvCouponUseRule= (TextView) itemView.findViewById(R.id.tv_coupon_use_rule);
            mIvCouponChose= (ImageView) itemView.findViewById(R.id.iv_coupon_chose);
            mTvCouponDate= (TextView) itemView.findViewById(R.id.tv_coupon_date);
            mTvShareOrder= (TextView) itemView.findViewById(R.id.tv_share_order);
            mTvCouponName= (TextView) itemView.findViewById(R.id.tv_coupon_name);
            mIvCanUse= (ImageView) itemView.findViewById(R.id.iv_can_use);
            mRlCouponContent= (RelativeLayout) itemView.findViewById(R.id.rl_coupon_content);
            mIvIsCanDetail= (ImageView) itemView.findViewById(R.id.iv_is_can_detail);
            mFlCouponMoney= (FrameLayout) itemView.findViewById(R.id.fl_coupon_money);
            mIvMoneyIcon= (ImageView) itemView.findViewById(R.id.iv_money_icon);
            mIvCouponUseRulePoint= (ImageView) itemView.findViewById(R.id.iv_coupon_use_rule_point);
            mIvCouponMoneyUsePoint= (ImageView) itemView.findViewById(R.id.iv_coupon_money_use_point);
            mLlCouponName= (LinearLayout) itemView.findViewById(R.id.ll_coupon_name);
            //动态设置大小
            StateUtils.doSetWidth(mContext,mFlCouponMoney);
            //点击事件
            mIvIsCanDetail.setOnClickListener(this);
            mTvShareOrder.setOnClickListener(this);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(isMyCoupon){//我的卡卷页面需要点击
                        return;
                    }
                    int pos= (int) v.getTag();
                    if(null!=mOnRecyclerViewItemClick){
                        mOnRecyclerViewItemClick.onRecyclerItemClick((RecyclerView) v.getParent(),pos);
                    }
                }
            });
        }

        @Override
        public void onClick(View v) {
            if(StringUtils.isNull(mHVCardBeans)){
                return;
            }
            int pos= (int) v.getTag();
            HVCardBean mCardBean=mHVCardBeans.get(pos);
            String mCouponId=mCardBean.getCouponId();
            switch (v.getId()){
                case R.id.iv_is_can_detail://点击查看详情
                    Intent mIntent=new Intent(mContext, CouponDetailActivity.class);
                    Bundle mBundle=new Bundle();
                    mBundle.putSerializable(Constants.KEY_CARD_BEAN,mCardBean);
                    mIntent.putExtras(mBundle);
                    mContext.startActivity(mIntent);
                    break;
                case R.id.tv_share_order:// 分享口令
                    if (null == mShareDialog) {
                        mShareDialog = new ShareDialog(mContext, R.style.BottomToTopAnim);
                    }
                    mShareDialog.setTitle(PicSplit.makeShareTitle(mCardBean.couponName));
                    String mShareGymUrl= PicSplit.makeShareUrl(PicSplit.S_COUPON_ID,mCouponId,null,null);
                    Log.e("shareUrl",mShareGymUrl);
                    mShareDialog.setTagUrl(mShareGymUrl);
                    mShareDialog.setShareContent(PicSplit.S_CARD_SHARE_CONTENT);
                    mShareDialog.show();
                    break;
            }
        }
    }

    private class HistoryViewHolder extends RecyclerView.ViewHolder{
         HistoryViewHolder(View itemView) {
            super(itemView);
        }
    }
}
